/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#ifndef __462A581B_65BA_411a_A1AD_A03350BA2B4A__
#define __462A581B_65BA_411a_A1AD_A03350BA2B4A__

#include "../tools/head.h"
#include "heart_acceptor.h"

/////////////////////////////////////////////////////////////////////////////////////////
// 

namespace xos_box
{
    // 
    // heart_impl
    // 

    class heart_impl : public xos_stl::mem_item< xos::release_impl< heart_impl >, thread_lock >,
        public i_heart
    {
	public:
		typedef heart_acceptor::LIST ACCEPTOR_LIST;
		typedef ACCEPTOR_LIST::ITER ACCEPTOR_ITER;
        typedef xos::release_impl< heart_impl > T;

    public:
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //
        static int get_xos_item_data( ITEM_DATA*& item_ptr );
        static int static_user_init();
        static int static_user_term();

    public:
        heart_impl();
        ~heart_impl();

    protected:
        heart_acceptor::T * find( f_acceptor pAcceptor );
        static void * thread( void * lpData );

        int init_data();
        int scan();

    protected:
		ACCEPTOR_LIST m_accept_list;
		thread_lock m_lock;
        int32_t m_iScanInterval;
        xos::i_thread * m_pThread;
        xos::i_event * m_pEvent;
        bool m_bQuit;

    public:
        int init();
        int term();

    public:
        // 
        // set heart check interval : ms
        // 
        int set_sys_check_interval( int32_t iSysCheckIntervalMS );

        // 
        // add acceptor : ms
        // 
        int add_acceptor( f_acceptor pAcceptor, int32_t iHeartIntervalMS );

        // 
        // remove acceptor
        // 
        int rem_acceptor( f_acceptor pAcceptor );

        //
        // start heart thread
        // 
        int start_heart_thread();
        int stop_heart_thread();
    };
} // xos

#endif // __462A581B_65BA_411a_A1AD_A03350BA2B4A__
